<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表表格 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
          width="300px"
        ></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"> </el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showEditAdrDialog"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location-outline"
              @click="showExpressInfoDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 编辑信息的对话框 -->
      <el-dialog
        title="编辑地址"
        :visible.sync="EditAdrDialogVisible"
        width="50%"
        @close="EditAdrFormClosed"
      >
        <el-form
          :model="EditAdrForm"
          :rules="EditAdrFormRules"
          ref="EditAdrFormRef"
          label-width="100px"
        >
          <el-form-item label="省市区/县" prop="firstAdr">
            <el-cascader
              v-model="EditAdrForm.firstAdr"
              :options="cityData"
              :props="AdrProps"
              @change="handleChange"
            ></el-cascader
          ></el-form-item>
          <el-form-item label="详细地址" prop="specificAdr">
            <el-input v-model="EditAdrForm.specificAdr"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="EditAdrDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="EditAdrDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 物流信息的对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="ExpressDialogVisible"
        width="50%"
      >
        <!-- 时间线 -->
        <el-timeline :reverse="reverse">
          <el-timeline-item
            v-for="(activity, index) in expressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>

        <span slot="footer">
          <el-button @click="ExpressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="ExpressDialogVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [],
      total: 0,
      EditAdrDialogVisible: false,
      EditAdrForm: {
        firstAdr: '',
        specificAdr: ''
      },
      EditAdrFormRules: {
        firstAdr: {
          required: true,
          message: '请输入活动名称',
          trigger: 'blur'
        },
        specificAdr: {
          required: true,
          message: '请输入活动名称',
          trigger: 'blur'
        }
      },
      cityData,
      AdrProps: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children'
      },
      reverse: true,
      ExpressDialogVisible: false,
      expressInfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取订单列表失败！')
      this.$message.success('获取订单列表成功！')
      this.orderList = res.data.goods

      console.log(res.data)
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    // 点击编辑按钮的事件
    showEditAdrDialog() {
      this.EditAdrDialogVisible = true
    },
    // 级联选择器变化触发的事件
    handleChange() {},

    // 编辑对话框的关闭事件
    EditAdrFormClosed() {
      this.$refs.EditAdrFormRef.resetFields()
    },
    // 点击物流信息按钮的事件
    async showExpressInfoDialog() {
      this.ExpressDialogVisible = true

      //获取物流信息
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200)
        return this.$message.error('获取物流信息失败！')
      this.$message.success('获取物流信息成功！')
      console.log(res.data)
      this.expressInfo = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
